package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.customer.CustomerInsertDTO;
import com.qk.dto.customer.CustomerListDTO;
import com.qk.entity.Customer;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.CustomerService;
import com.qk.vo.customer.CustomerListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * author：zzq
 * createTime：2025/10/17 19:54
 * description： 
 */
@Service
@Slf4j
@SuppressWarnings("all")
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
    @Override
    public PageResult<CustomerListVO> customerList(CustomerListDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<CustomerListVO> page = (Page<CustomerListVO>) this.baseMapper.selectCustomerList(dto);
        return PageResult.<CustomerListVO>builder()
                .total(Long.valueOf(page.getTotal()).intValue())
                .rows(page.getResult())
                .build();
    }

    @Override
    public void addCustomer(CustomerInsertDTO dto) {
        Customer customer = BeanUtil.copyProperties(dto, Customer.class);
        //补全信息
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        try {
            this.baseMapper.insert(customer);
        } catch (Exception e) {
            log.error("添加客户失败", e);
        }
    }
}
